package com.gzs.learn.payment.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.gzs.learn.common.util.NumberUtil;
import com.gzs.learn.inf.BaseResponseDto;
import com.gzs.learn.inf.PageResponseDto;
import com.gzs.learn.payment.inf.dto.web.PaymentList;
import com.gzs.learn.payment.inf.dto.web.req.PaymentSearchDto;
import com.gzs.learn.payment.mapper.PaymentMapper;
import com.gzs.learn.payment.model.po.Payment;
import com.gzs.learn.payment.ms.PaymentMS;
import com.gzs.learn.payment.service.IPaymentWebService;
import com.gzs.learn.payment.util.PageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支付请求Service业务层处理
 *
 * @author guanzhisong
 * @date 2021-08-14
 */
@Service
@RequiredArgsConstructor
public class PaymentWebServiceImpl implements IPaymentWebService {
	private final PaymentMapper paymentMapper;

	/**
	 * 查询支付请求列表
	 *
	 * @param searchDto 支付请求
	 * @return 支付请求
	 */
	@Override
	public PageResponseDto<PaymentList> searchPaymentList(PaymentSearchDto searchDto) {
		Page<?> page = new Page<>(searchDto.getPageNum(), searchDto.getPageSize());
		IPage<Payment> pageInfo = paymentMapper.selectPaymentList(page, searchDto);
		List<Payment> payments = pageInfo.getRecords();
		List<PaymentList> paymentLists = Lists.newArrayListWithExpectedSize(payments.size());
		for (Payment payment : payments) {
			PaymentList paymentList = PaymentMS.INST.fromPayment(payment);

			paymentList.setAmt(NumberUtil.fen2yuan(payment.getAmt(), 2));
			paymentLists.add(paymentList);
		}
		return PageUtil.buildPageResponse(paymentLists, (int) pageInfo.getTotal(), page);
	}

	@Override
	public BaseResponseDto<PaymentList> paymentDetail(Long serial) {
		if (serial == null || serial <= 0) {
			return BaseResponseDto.bizError("serial:" + serial + " is illegal");
		}
		Payment payment = paymentMapper.selectPaymentById(serial);
		PaymentList paymentList = PaymentMS.INST.fromPayment(payment);
		return BaseResponseDto.success(paymentList);
	}
}
